package com.example.demo.service;

import com.example.demo.entity.Comment;
import com.example.demo.entity.CommentDTO;
import com.example.demo.mapper.CommentMapper;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserService userService;

    // 添加评论
    public void addComment(Comment comment) {
        commentMapper.insertComment(comment);
    }

    // 获取帖子的评论列表（只返回一级评论）
    public List<CommentDTO> getCommentsByPostId(Long postId, HttpServletRequest request) {
        // 获取当前登录用户
        var currentUser = userService.getCurrentUser(request);

        // 查询帖子的评论列表，只查询 parentCommentId 为空的评论
        List<Comment> comments = commentMapper.getCommentsByPostId(postId);

        // 转换为 CommentDTO 列表
        return comments.stream()
                .filter(comment -> comment.getParentCommentId() == null) // 只保留 parentCommentId 为空的评论
                .map(comment -> {
                    CommentDTO commentDTO = new CommentDTO();
                    commentDTO.setId(comment.getId());
                    commentDTO.setContent(comment.getContent());
                    commentDTO.setCreatedAt(comment.getCreatedAt());

                    // 获取评论的作者信息
                    var author = userService.findById(comment.getUserId());
                    commentDTO.setUsername(author.getUsername());
                    commentDTO.setAvatar(author.getAvatar());

                    // 如果当前用户是评论的作者，标记为 "author"
                    if (currentUser != null && currentUser.getId().equals(comment.getUserId())) {
                        commentDTO.setAuthor(true);
                    } else {
                        commentDTO.setAuthor(false);
                    }

                    return commentDTO;
                })
                .collect(Collectors.toList());
    }

    // 获取二级评论
    public List<CommentDTO> getRepliesByParentCommentId(Long parentCommentId) {
        List<Comment> replies = commentMapper.getRepliesByParentCommentId(parentCommentId);
        return replies.stream().map(reply -> {
            CommentDTO commentDTO = new CommentDTO();
            commentDTO.setId(reply.getId());
            commentDTO.setContent(reply.getContent());
            commentDTO.setCreatedAt(reply.getCreatedAt());

            // 获取评论的作者信息
            var author = userService.findById(reply.getUserId());
            commentDTO.setUsername(author.getUsername());
            commentDTO.setAvatar(author.getAvatar());

            return commentDTO;
        }).collect(Collectors.toList());
    }

    // 获取帖子的评论总数
    public int getCommentCountByPostId(Long postId) {
        return commentMapper.getCommentCountByPostId(postId);
    }

    // 获取帖子的二级评论总数
    public int getReplyCountByPostId(Long postId) {
        return commentMapper.getReplyCountByPostId(postId);
    }
}
